package com.uphone.kingmall.activity.personal.set;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.china.zhuoyue.R;
import com.lzy.okgo.model.Response;
import com.tencent.connect.common.Constants;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.uphone.kingmall.app.MyApplication;
import com.uphone.kingmall.base.BaseActivity;
import com.uphone.kingmall.base.BaseUiListener;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.utils.AppUtil;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.ToastUtils;
import com.uphone.kingmall.wxapi.WXEntryActivity;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * <pre>
 *     author : 王雨
 *     e-mail : sunzengyan@outlook.com
 *     time   : 2018/07/31
 *     desc   :绑定账号
 *     version: 1.0
 * </pre>
 */
public class BindAccountActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_qq_status)
    TextView tvQqStatus;
    @BindView(R.id.rl_qq)
    RelativeLayout rlQq;
    @BindView(R.id.tv_wx_status)
    TextView tvWxStatus;
    @BindView(R.id.rl_wx)
    RelativeLayout rlWx;
    private IUiListener loginListener;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_bind_account;
    }

    @Override
    public void initView() {
        setStatusBar();
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData();
    }

    public void setQQBind() {
        tvQqStatus.setText("已绑定");
        tvQqStatus.setSelected(true);
    }

    public void setWChatBind() {
        tvWxStatus.setText("已绑定");
        tvWxStatus.setSelected(true);
    }

    private void loadData() {
        OkGoUtils.normalRequest(MyUrl.getUserBindInfo, null, new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                if (code == 0) {
                    try {
                        JSONObject jsonObject = new JSONObject(body);
                        if (code == 0) {
                            String qqNo = jsonObject.getString("qqNo");
                            if (TextUtils.isEmpty(qqNo)) {
                                tvQqStatus.setText("未绑定");
                                tvQqStatus.setSelected(false);
                            } else {
                                tvQqStatus.setText("已绑定");
                                tvQqStatus.setSelected(true);
                            }
                            String wxNo = jsonObject.getString("wxNo");
                            if (TextUtils.isEmpty(wxNo)) {
                                tvWxStatus.setText("未绑定");
                                tvWxStatus.setSelected(false);
                            } else {
                                tvWxStatus.setText("已绑定");
                                tvWxStatus.setSelected(true);
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    @Override
    public void initData() {

    }


    @OnClick({R.id.iv_back, R.id.rl_qq, R.id.rl_wx})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.rl_qq:
                if (!tvQqStatus.isSelected()) {
                    AppUtil.checkQQ(this, new AppUtil.AppCheckCallback() {
                        @Override
                        public void callBack() {
                            if (loginListener == null) {
                                loginListener = new BaseUiListener(BindAccountActivity.this, BaseUiListener.TYPE_BIND);
                            }
                            MyApplication.qqTencent.login(BindAccountActivity.this, "all", loginListener);
                        }
                    });
                } else {
                    ToastUtils.showShortToast(this, "您已绑定QQ号");
                }
                break;
            case R.id.rl_wx:
                if (!tvWxStatus.isSelected()) {
                    try {
                        AppUtil.checkWchat(this, new AppUtil.AppCheckCallback() {
                            @Override
                            public void callBack() {
                                WXEntryActivity.LoginType = 1;
                                SendAuth.Req req = new SendAuth.Req();
                                req.scope = "snsapi_userinfo";
                                req.state = "wechat_sdk_demo_test";
                                MyApplication.api.sendReq(req);
                            }
                        });
                    } catch (Exception e) {
                        LogUtils.e("微信绑定异常:" + e.toString());
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showShortToast(this, "您已绑定微信号");
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Constants.REQUEST_LOGIN ||
                requestCode == Constants.REQUEST_APPBAR) {
            if (loginListener != null) {
                Tencent.onActivityResultData(requestCode, resultCode, data, loginListener);
            }
        }
    }
}
